proc=Proc.newdo|name|puts"Thankyou#{name}!"enddefthankyieldendproc.call#outputnothing,justfineproc.call('God')#=>ThankyouGod!thank&proc#outputnothing,too.Fine;thank&proc('God')#Error!thank&proc.call('God')#Error!thankproc.call('God')#Error!#So,whatshouldIdoifIhavetopassthe'God'totheprocandusethe
这是我现在所拥有的,它有点管用:defpadding(a,b,c=nil)untila[b-1]a这是它起作用的时候:a=[1,2,3]padding(a,10,"YES")=>[1,2,3,"YES","YES","YES","YES","YES","YES","YES"]a[1,2,3]padding(a,10,1)=>[1,2,3,1,1,1,1,1,1,1]但是当我没有为“c”输入值时它崩溃了a=[1,2,3]padding(a,10)Killed我应该如何附加它以避免崩溃?此外,您建议如何更改此方法以按如下方式使用它:[1,2,3].padding(10)=>[1,2,3,n
我有一个散列,我想将其中的值用作新散列中的键,该新散列包含该项目在原始散列中作为值出现的次数的计数。所以我使用:hashA.keys.eachdo|i|putshashA[i]end示例输出:0112011我希望新的哈希如下:{0=>2,1=>4,2=>1} 最佳答案 counts=hashA.values.inject(Hash.new(0))do|collection,value|collection[value]+=1collectionend 关于Ruby"count"哈希方法,